//
//  BeanTable.m
//  storeapp
//
//  Created by huangyi on 13-4-17.
//  Copyright (c) 2013年 ios. All rights reserved.
//

#import "BeanTable.h"
#import "Bean.h"

#import "FMDatabase.h"
#import "FMDatabaseAdditions.h"
#import "FMDatabasePool.h"
#import "FMDatabaseQueue.h"

@implementation BeanTable

-(int)count{
    int row=0;
    FMDatabase *db=[self.db getFMDB];
    NSString *sqlstr=[NSString stringWithFormat:@"select count(id) from %@",self.tableName];
    FMResultSet *set=[db executeQuery:sqlstr];
    while ([set next]) {
        NSNumber *number=[set objectForColumnIndex:0];
        row=number.intValue;
    }
    return row;
}
-(BeanObject*)dispense{
    BeanObject *obj=[[BeanObject alloc] init];
    obj.table=self;
    return obj;
}
-(BeanObject*)load:(int)rowid{
    BeanObject *obj=[self findOne:@"id = %i",rowid,nil];
    return obj;
}
-(BeanObject*)findOne:(NSString*)condition,...{
    NSString *sql=nil;
	va_list args;
	if (condition)
	{
		va_start(args, condition);
        sql=[[NSString alloc] initWithFormat:condition arguments:args];
		va_end(args);
	}
    NSDictionary *result=nil;
    FMDatabase *db=[self.db getFMDB];
    NSString *sqlstr=[NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@",self.tableName,sql];
    FMResultSet *set=[db executeQuery:sqlstr];
    BeanObject *obj=nil;
    if ([set next]) {
        result=[set resultDictionary];
        obj=[self dispense];
        obj.bean=[NSMutableDictionary dictionaryWithDictionary:result];
    };
    return obj;
}
-(NSArray*)findAll:(NSString*)condition,...{
    NSString *sql=nil;
	va_list args;
	if (condition)
	{
		va_start(args, condition);
        sql=[[NSString alloc] initWithFormat:condition arguments:args];
		va_end(args);
	}
    NSMutableArray *array=[NSMutableArray array];
    FMDatabase *db=[self.db getFMDB];
    NSString *sqlstr=[NSString stringWithFormat:@"SELECT * FROM %@ WHERE %@",self.tableName,sql];
    FMResultSet *set=[db executeQuery:sqlstr];
    while ([set next]) {
        BeanObject *obj=[self dispense];
        NSDictionary *result=[set resultDictionary];
        obj.bean=[NSMutableDictionary dictionaryWithDictionary:result];
        [array addObject:obj];
    };
    return array;
}
-(BeanObject*)findOrDispense:(NSString*)condition,...{
    NSString *sql=nil;
	va_list args;
	if (condition)
	{
		va_start(args, condition);
        sql=[[NSString alloc] initWithFormat:condition arguments:args];
		va_end(args);
	}
    BeanObject *obj=[self findOne:sql, nil];
    if (!obj) {
        obj=[self dispense];
    }
    return obj;
}
-(BOOL)wipe{
    FMDatabase *db=[self.db getFMDB];
    NSString *sqlstr=[NSString stringWithFormat:@"DELETE FROM %@ WHERE 1",self.tableName];
    BOOL result=[db executeUpdate:sqlstr];
    return result;
}
-(NSArray*)getFieldNames{
    FMDatabase *db=[self.db getFMDB];
    FMResultSet *set=[db getTableSchema:self.tableName];
    NSMutableArray *fields=[NSMutableArray array];
    while ([set next]) {
        [fields addObject:set.resultDictionary[@"name"]];
    }
    return fields;
}

-(BeanObject*)convert:(NSDictionary*)dictionary{
    BeanObject *obj=[self dispense];
    obj.bean=[NSMutableDictionary dictionaryWithDictionary:dictionary];
    return obj;
}

@end
